<?php

namespace App\Models;

// use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Course extends Model
{
    protected $fillable = ['name', 'code','teacher_id', 'status', 'category_id'];

    // 关联分类
    public function category()
    {
        return $this->belongsTo(CourseCategory::class, 'category_id');
    }
    // 关联授课教师
    public function teacher()
    {
        return $this->belongsTo(Admin::class, 'teacher_id');
    }

    // 关联知识（多对多）
    public function knowledges()
    {
        return $this->belongsToMany(Knowledge::class, 'course_knowledges');
    }

    // 关联试卷（多对多）
    public function papers()
    {
        return $this->belongsToMany(Paper::class, 'course_papers');
    }

    // 关联学生
    public function students()
    {
        return $this->belongsToMany(Student::class, 'student_courses')
                    ->withPivot('start_time', 'progress')
                    ->withTimestamps();
    }

    // 关联实训参数（一对一）
    public function practicalParam()
    {
        return $this->hasOne(PracticalParam::class);
    }

    // 统计关联数量
    public function getKnowledgeCountAttribute()
    {
        return $this->knowledges()->count();
    }
    public function getPaperCountAttribute()
    {
        return $this->papers()->count();
    }
    public function getStudentCountAttribute()
    {
        return $this->students()->count();
    }
}
